package org.zyj.control;

import java.util.List;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.zyj.entity.Catagory;
import org.zyj.entity.Product;
import org.zyj.service.ProductService;
import org.zyj.utils.PageBean;

@Controller
public class ProductController {

	@Autowired
	private ProductService productService;
	
	@RequestMapping("/item")
	public String index(Integer current,Model model) {	
		PageBean<Product> pageBean = productService.findPageProduct(current);
		model.addAttribute("pagebean", pageBean);		
		return "index";
	}
	
	@RequestMapping("/item/del/{id}")
	public String delete(@PathVariable String id) {
		Integer count  = productService.delete(id);
		return "redirect:/item";
	}
	
	@RequestMapping("/manager/add")
	public String add(Model model) {
		
		List<Catagory> listAllCatagory = productService.listAllCatagory(); 
		
		model.addAttribute("clist", listAllCatagory);
		
		return "add";
	}
	
	@RequestMapping("item/insert")
	public String insert(Product product) {
		
		if(product.getType() == null) {
			product.setType(2);
		}
		
		Integer count = productService.insert(product);
		return "redirect:/item";
	}
	
}
